def plugin(request, config=[], types=[]):
    context = {}
    
    for type in types:
        plugins = [plugin for plugin in config if plugin['type'] == type]
        context[type] = get_plugins_context(request, plugins)
    
    return context

def get_plugins_context(request, plugins):
    context = []
    for plugin in plugins:
        if plugin.has_key('package'):
            plugin_module = __import__(plugin['package'], globals(), locals(), ['main'], -1)
            data = plugin_module.main(request)
        else:
            data = None
        plugin['view_path'] = plugin['view'] 
        context.append({'config':plugin, 'data':data})
    
    return context  
